<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  * {
    margin: 0;
    padding: 0;
    box-sizing: content-box;
  }

  a {
    text-decoration: none;
    color: #721c24;
  }

  h1 {
    text-align: center;
    color: #333;
    margin: 20px 0;
  }

  table {
    margin: 0 auto;
    width: 800px;
    border-collapse: collapse;
    color: #004085;
  }

  th {
    padding: 10px;
    background: #cfe5ff;
    font-size: 20px;
    font-weight: 400;
  }

  td,
  th {
    border: 1px solid #b8daff;
  }

  td {
    padding: 10px;
    font-size: 16px;
    text-align: center;
    color: #666;
  }

  tbody tr {
    background: #fff;
  }

  tbody tr:hover {
    background: #e1ecf8;
  }

  .info {
    width: 900px;
    text-align: center;
    margin: 50px auto;
  }

  .info input,
  .info select {
    width: 80px;
    height: 27px;

    border-radius: 5px;
    border: 1px solid #b8daff;
    padding-left: 5px;

    margin-right: 15px;
  }

  .info button {
    width: 60px;
    height: 27px;
    background-color: #004085;

    border: 0;
    color: #fff;

    border-radius: 5px;
  }

  .info .age {
    width: 50px;
  }
</style>

<body>
  <h1>新增学员</h1>
  <form class="info" autocomplete="off">
    姓名：<input type="text" class="uname" name="uname" />
    年龄：<input type="text" class="age" name="age" />
    性别: <select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text" class="salary" name="salary" />
    就业城市：<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody> </tbody>
  </table>
</body>
<script>
  const initData = [
    {
      stuId: 1,
      uname: '迪丽热巴',
      age: 22,
      gender: '女',
      salary: '20000',
      city: '上海',
      time: '2099/9/9 08:08:08'
    }
  ]

  //localStorage.setItem('data', JSON.stringify(initData))
  const arr = JSON.parse(localStorage.getItem('data')) || []

  const tbody = document.querySelector('tbody')
  function render() {
    const trArr = arr.map(function (ele, index) {
      return `
          <tr>
            <td>${ele.stuId}</td>
            <td>${ele.uname}</td>
            <td>${ele.age}</td>
            <td>${ele.gender}</td>
            <td>${ele.salary}</td>
            <td>${ele.city}</td>
            <td>${ele.time}</td>
            <td>
              <a href="javascript:" data-id={"${index}"}>
                <i class="iconfont icon-shanchu"></i>
                删除
              </a>
            </td>
          </tr> `
    })
    console.log(trArr)
    tbody.innerHTML = trArr.join('')
  }

  render()

  //增加
  const info = document.querySelector('.info')
  const uname = document.querySelector('.uname')
  const age = document.querySelector('.age')
  const salary = document.querySelector('.salary')
  const city = document.querySelector('.city')
  const gender = document.querySelector('.gender')


  info.addEventListener('submit', function (e) {
    //e.preventDefault()
    if (!uname.value || !age.value || !salary.value) {
      return alert('请填写完整信息')
    }
    arr.push({
      stuId: arr.length ? arr[arr.length - 1].stuId + 1 : 1,
      uname: uname.value,
      age: age.value,
      gender: gender.value,
      city: city.value,
      salary: salary.value,
      time: new Date().toLocaleString(),
    })
    render()
    //this.reset()
    //localStorage.setItem('data', JSON.stringify(arr))
  })

  //删除
  tbody.addEventListener('click', function (e) {
    if (e.target.tagName === 'A') {
      if (confirm('确定删除吗?'))
        arr.splice(e.target.dataset.id, 1)
      render()
    }
  })
</script>

</html>